<?php
/**
 * Created by PhpStorm.
 * User: zlc
 * Date: 2018-07-19
 * Time: 上午 11:43
 * 意见反馈
 */
namespace app\admin\controller;
use app\admin\model\FeedBackModel;
class Feedback extends Base{
    public function index(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['phone'])) {
                $where['snake_member.member_mobile'] = ['like', '%' . $param['phone'] . '%'];
            }
            if (!empty($param['type'])) {
                $where['snake_feedback.type'] = $param['type'];
            }
            if (!empty($param['name'])) {
                $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
            }
            $join = "snake_member";
            $condition="snake_feedback.member_id = snake_member.member_id";
            $feedback =  new FeedBackModel();
            $selectResult = $feedback->getFeedbackByWhere($where, $offset, $limit,$join,$condition);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['checked'] = "<input type='checkbox' name='check' value='".$vo['id']."'>";
                $selectResult[$key]['time'] = date('Y-m-d H:i:s',$vo['time']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $feedback->getFeedbackCount($where,$join,$condition);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }

    public function feedbackDel(){
        $feedback = new FeedBackModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'删除失败');
        }
        if($param['type'] == 1) {
            $map = " id in (" . implode(',', explode('8', $param['id'])) . ")";
        }else{
            $map = " id = ".$param['id'];
        }
        $res = $feedback->getFeedbackDel($map);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'feedback/feedbackdel',
                'href' => "javascript:feedbackDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}